package com.interview.javabase.algorithm.hand.读写锁;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Stream;

/**
 * 手写读写锁
 *
 * @author zhangshguang
 * @date 2020/07/27
 */
public class ReadWriteLockDemo {

    ReentrantReadWriteLock  readWriteLock = new ReentrantReadWriteLock();

    public int index = 0;

    public void write() throws InterruptedException {
        readWriteLock.writeLock().lock();
        Thread.sleep(100);
        index++;
        System.out.println(index);
        readWriteLock.writeLock().unlock();
    }

    public void read() throws InterruptedException {
        readWriteLock.readLock().lock();
        System.out.println(index);
        Thread.sleep(5000);
        readWriteLock.readLock().unlock();
    }

    public static void main(String[] args) throws InterruptedException {

        String a = "00";

        System.out.println("061".startsWith(a));
        ReadWriteLockDemo readWriteLockDemo = new ReadWriteLockDemo();
//
//        List<Integer> list = new ArrayList<>();
//        list.add(2);
//        list.add(1);
//        list.sort(Comparator.comparingInt(o -> o));
//
//        list.forEach(System.out::println);
//
//
//        Integer i = 1;
//        boolean f = i == 0;
//        for (int i = 0; i < 5; i++) {
//            new Thread(()->{
//                try {
//                    readWriteLockDemo.read();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }).start();
//        }
//
//        for (int i = 0; i < 1000; i++) {
//            new Thread(()->{
//                try {
//                    readWriteLockDemo.write();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }).start();
//        }
//        System.out.println(handReadWriteLock.index);
//        Thread.sleep(90000);

    }
}
